/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Accountant;

/**
 *
 * @author Adriano
 */
public class AccountantDAO implements DAO {

    private DataSource dataSource;

    public AccountantDAO() {
        dataSource = new DataSource();
    }

    public void create(Object o) {
        try {

            String sql = "INSERT INTO accountant VALUES (null,?,?,?,?,?)";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Accountant contador = (Accountant) o;

            
            ps.setString(1, contador.getTxtNome());
            ps.setString(2, contador.getTxtCpf());
            ps.setString(3, contador.getTxtCrc());
            ps.setString(4, contador.getLstUF());
            ps.setString(5, contador.getLstAssinatura());

            int res = ps.executeUpdate();
            ps.close();
            if (res == 0) {
                System.err.println("CONTADORDAO.CREATE - Nenhum contador criado !");

            }
        } catch (SQLException e) {
            System.err.println("CONTADORDAO.CREATE  - " + e.getMessage());

        }
    }

    public Object read(Object o) {

        try {

            String sql = "SELECT * FROM accountant WHERE crc = ? ";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Accountant query = (Accountant) o;

            ps.setString(1, query.getTxtNome());
            ps.setString(2, query.getTxtCrc());
            ps.setString(3, query.getTxtCpf());
            ps.setString(4, query.getLstUF());
            ps.setString(5, query.getLstAssinatura());
            ps.setInt(6, query.getTxtId());

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                Accountant contador = new Accountant();

                contador.setTxtId(rs.getInt("Id"));
                contador.setTxtNome(rs.getString("Nome"));
                contador.setTxtCpf(rs.getString("CPF"));
                contador.setTxtCrc(rs.getString("CRC"));
                contador.setLstUF(rs.getString("UF"));
                contador.setLstAssinatura(rs.getString("Assinatura"));
                contador.setTxtId(rs.getInt("ID"));

                return contador;
            }
        } catch (SQLException e) {
            System.err.println("CONTADORDAO.READ - " + e.getMessage());
        }
        return null;
    }

    public void delete(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Object o) {

        try {

            String sql = "UPDATE  accountant SET nome=? ,cpf= ? ,crc=?, uf=? , assinatura=?  WHERE id = ? ";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            Accountant query = (Accountant) o;

            ps.setString(1, query.getTxtNome());
            ps.setString(2, query.getTxtCpf());
            ps.setString(3, query.getTxtCrc());
            ps.setString(4, query.getLstUF());
            ps.setString(5, query.getLstAssinatura());
            ps.setInt(6, query.getTxtId());

            ps.executeUpdate();

        } catch (SQLException e) {
            System.err.println("UPDATEACCOUNTANT - " + e.getMessage());
        }

    }

    public Object find(Object o) {

        try {

            String sql = "SELECT * FROM accountant WHERE cpf = ?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Accountant query = (Accountant) o;

            ps.setString(1, query.getTxtCpf());

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                Accountant contador = new Accountant();
                contador.setTxtNome(rs.getString("Nome"));
                contador.setTxtCpf(rs.getString("CPF"));
                contador.setTxtCrc(rs.getString("CRC"));
                contador.setLstAssinatura(rs.getString("Assinatura"));
                contador.setLstUF(rs.getString("UF"));
                contador.setTxtId(rs.getInt("ID"));

                return contador;
            }
        } catch (SQLException e) {
            System.err.println("BUSCACONTADORDAO.READ - " + e.getMessage());
        }
        return null;

    }

    public Object findAll() {
        try {

            String sql = "SELECT * FROM accountant";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            ArrayList<Accountant> listaContador = new ArrayList<Accountant>();

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                Accountant cont = new Accountant();

                cont.setTxtId(rs.getInt("Accountant.id"));
                cont.setTxtNome(rs.getString("Accountant.nome"));
                cont.setTxtCpf(rs.getString("Accountant.cpf"));
                cont.setTxtCrc(rs.getString("Accountant.crc"));
                cont.setLstAssinatura(rs.getString("Accountant.assinatura"));
                cont.setLstUF(rs.getString("Accountant.uf"));

                listaContador.add(cont);

            }
            return listaContador;

        } catch (Exception e) {
            System.err.println("CONTADORFINDALL.READ - " + e.getMessage());
            return null;
        }

    }

    public void closeDAO() {
        dataSource.closeConnection();
    }
}
